package com.hrt.job;

import org.quartz.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 要执行的定时的任务类 实现job接口，重写方法execute()
 */
public class HelloJob implements Job {

    /**
     * 定时任务要执行的内容，一旦触发定时操作，那么这个任务自动执行！调用execute方法
     * @param context
     * @throws JobExecutionException 任务调度的上下文对象 ,封装正在执行的这个任务的相关运行信息！
     */
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //通过上下文获取下一次触发的时间
        Date nextFireTime = context.getNextFireTime();
        //通过上下文获取JobDetail对象
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        Object message = jobDataMap.get("message");

        //通过上下文获取Trigger
        Trigger trigger = context.getTrigger();
        JobDataMap jobDataMap1 = trigger.getJobDataMap();
        Object msg = jobDataMap1.get("msg");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("阻塞前:"+format.format(new Date())+"线程:"+Thread.currentThread().getName());
        // 通过设置任务阻塞可以知道,quartz不考虑该任务执行多久,只要到时间了就会用多线程启动
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String nowTime=format.format(new Date());

        System.out.println("各位老铁，早上好!!!"+nowTime+"下一次触发:"+nextFireTime+",message="+message+",msg="+msg);
    }
}
